<html lang="en">
<head>
    <title>地区管理</title>
    <#include "../common/header.ftl">
    <script type="text/javascript" src="/js/plugins/bootstrap-treeview/bootstrap-treeview.js"></script>
    <link rel="stylesheet" href="/js/plugins/bootstrap-treeview/bootstrap-treeview.css" type="text/css" />
    <script>

        /*treeview格式,数据传输过来需要使用指定的格式才能出现效果*/
        $(function () {
            $.post("/region/getRegionByParentId.do",function (parentData) {
                $('#treeview').treeview({
                    color: "#428bca",
                    showTags: true,
                    data: [{text:"全部地区", nodes: parentData}],
                    //图标中的 + 号,响应事件
                    lazyLoad: function (node) {
                        $.post("/region/getRegionByParentId.do",{"parentId":node.id},function (sunData){
                            //给父节点增加子节点
                            $('#treeview').treeview('addNode', [ sunData, node ]);
                        });
                    },
                    //节点选中事件
                    onNodeSelected: function(event, node){
                        $.post("/region/getRegionByParentId.do",{"parentId":node.id},function (sunData){
                            var html = "";
                            /*将返回的对象*/
                            $.each(sunData,function (index,item) {
                                var a = "<button type='button' class='btn btn-primary editBtn' data-json='"+ item.json +"'>编辑</button>";
                                html += "<tr><td>"+ (index + 1) +"</td><td>"+ item.text +"</td><td>"+ a +"</td></tr>";
                            });
                            $('#tbody').html(html);
                        });
                    }
                });
            });
           /* $('.editBtn').click(function () {
                $('#editModal').modal("show");
            });*/
            $('#tbody').on("click",".editBtn",function () {
                var json = $(this).data("json");
                console.log(json);
                if(json){
                   $("#editForm input[name = 'parentName']").val(json.pname);
                    $("#editForm input[name = 'parent.id']").val(json.pid);
                    $("#editForm input[name = 'name']").val(json.text);
                   $("#editForm input[name = 'id']").val(json.id);
                   $("#editForm input[name = 'state']").val(json.state);
                }
                $('#editModal').modal("show");
            });

            $("#addRegionBtn").click(function () {
                //获取被选中的节点数组,直接获得第一个json对象
                var node = $('#treeview').treeview('getSelected')[0];
                if (node){
                    //已选中的情况,将上级id和name回显
                    $("#editForm input[name = 'parentName']").val(node.text);
                    $("#editForm input[name = 'parent.id']").val(node.id);
                    console.log(node);
                }else{
                    //未选中的情况
                    $("#editForm input[name = 'parentName']").val("顶级目录");
                }
                $('#editModal').modal("show");
            });

            $(".submitBtn").click(function () {
                $("#editForm").ajaxSubmit(function (data) {
                    if(data.success){
                        window.location.reload();
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                });
            });
        });
    </script>

</head>
<body>
<!--左侧菜单回显变量设置-->
<#assign currentMenu="region">
<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-2">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-10">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">地区管理</h1>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-4">
                    <a href="javascript:void(-1);" class="btn btn-success" id="addRegionBtn">添加地区</a>
                    <div id="treeview"></div>
                </div>
                <div class="col-sm-8">
                    <table class="table table-striped table-hover">
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>名称</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="tbody">
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<!---部门编辑--->
<div class="modal fade" id="editModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">地区编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" value="" name="id">
                    <div class="form-group" >
                        <label  class="col-sm-3 control-label">上级地区：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="parentName" value="" readonly>
                            <input type="hidden" class="form-control" name="parent.id" value="" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-3 control-label">地区名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control"  name="name" value="" placeholder="请输入地区名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label  class="col-sm-3 control-label">状态：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="state" name="state">
                                <option value="0">不推荐</option>
                                <option value="1">推荐</option>
                            </select>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary submitBtn">保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal" >取消</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>